<?php
/*
 * Created on 13-sept.-2006
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
class CameraController extends Zend_Controller_Action
{
    //// DECLARATION DES FONCTIONS PRIVEES ////  
    private function myTracking($myThreadId)
    {
    	if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/track/status') ){
            if ( ereg(" Not Enabled", $myResponse[0]) )
            {
                return false;
            } else 
            {
            	return true;        
            }                                                         
        } 
    }
    
    private function myAutoTrack($myThreadId)
    {
        if ( $this->myTracking($myThreadId) )
        {
            if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/track/status') ){
                if ( ereg(" enabled", $myResponse[1]) )
                {
                    return "enabled";
                } else
                {
                    return "disabled";
                }            
            }         	
        } else return false;	    
    }
    
    
    //// DECLARATION DES FONCTIONS PUBLIQUES ////
    public function indexAction()
    {
    	$get = Zend::registry('fGet');
        $session = Zend::registry('fSession');
        $acl = Zend::registry('acl');  

        if ( $myThreadId = $get->getRaw('threadId') )
        {                                  
            if ( $myThreadPath = threadPath($myThreadId) )   
            {            	                
                if ($myThreadSize = $get->getRaw('threadSize'))
                {
                    $myTblSize = explode('x',$myThreadSize);
                    $myThreadWidth = $myTblSize[0];
                    $myThreadHeight = $myTblSize[1];
                }else
                {
                	$myThreadWidth = "";
                    $myThreadHeight = "";
                }
                $db = Zend::registry('db');
                $sql = "SELECT name FROM threads WHERE id = '" . $myThreadId . "'";
                $myThreadName = $db->fetchOne($sql);
                
                $myThreadStatus = threadStatus($myThreadId);
                $myThreadTracking = $this->myTracking($myThreadId);
                $myThreadAutoTrack = $this->myAutoTrack($myThreadId);
                
                // SELECTION DU NOMBRE DE SEQUENCES ENREGISTREES NON LU //
                $sql = "SELECT count(filename) FROM `security` WHERE `file_type` = '8' " .
                        "AND lu = 'N' AND `camera` = '" . $myThreadId . "'";                
                $myFilesCount = $db->fetchOne($sql);         
                
                //SELECTION DU NOMBRE DE SEQUENCES ENREGISTREES LU //
                $sql = "SELECT count(filename) FROM `security` WHERE `file_type` = '8' " .
                        "AND lu = 'O' AND `camera` = '" . $myThreadId . "'";                
                $myArchivesCount = $db->fetchOne($sql);    
                
                //SELECTION DES SNAPSHOTS PRIS //
                $sql = "SELECT count(filename) FROM security WHERE camera = '" . $myThreadId . "' " .
                        "AND file_type = '2'";
                $mySnapshotCount = $db->fetchOne($sql);
                
                $myAllowed = $acl->isAllowed($session->groupe, null, 'consult');

                $view = Zend::registry('view');
                $view->threadId = $myThreadId;
                $view->threadName = $myThreadName;
                $view->threadPath = $myThreadPath;  
                $view->threadWidth = $myThreadWidth;
                $view->threadHeight = $myThreadHeight;
                $view->threadStatus = $myThreadStatus;
                $view->threadTracking = $myThreadTracking;
                $view->threadAutoTrack = $myThreadAutoTrack;
                $view->fileCount = $myFilesCount;   
                $view->archiveCount = $myArchivesCount;   
                $view->snapshotsCount = $mySnapshotCount; 
                $view->userAllowed = $myAllowed;   
                echo $view->render('cameraView.php');
            } else $this->_redirect('/accueil');                             
        }                
    }
    public function fullScreenAction()
    {
    	$get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');
        
        $db = Zend::registry('db');
        $sql = "SELECT name FROM threads WHERE id = '" . $myThreadId . "'";
        $myThreadName = $db->fetchOne($sql);
        
        if ( $myThreadPath = threadPath($myThreadId) ){
            $view = Zend::registry('view');
            $view->threadId = $myThreadId;
            $view->threadName = $myThreadName;
            $view->threadPath = $myThreadPath;  
            echo $view->render('cameraFullScreenView.php');        	
        } else die();
    }
    
    public function detectionAction()
    {
        $get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');
        $myAction = $get->getRaw('action');                       
                       
        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/detection/' . $myAction) ){        	                                                                     
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else return false;
    }
    
    public function snapshotAction()
    {
    	$get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');  
              
        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/action/snapshot') ){                                                         
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else $this->_redirect('/');
    }
    
    public function makeMovieAction()
    {
    	$get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');
                
        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/action/makemovie') ){                                                         
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else $this->_redirect('/');
    }
    
    public function autoTrackAction()
    {
    	$get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');
        $myAutoTrack = $get->getRaw('auto');
        
        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/track/auto?value=' . $myAutoTrack) ){                                                         
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else $this->_redirect('/');
    }
    
    public function moveTrackAction()
    {
    	//http://motionhost.mydomain.tld:port/2/track/set?pan=10&tilt=-5
        $get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');
        $myTrackPan = $get->getRaw('pan');
        $myTrackTilt = $get->getRaw('tilt');
                       
        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/track/set?pan=' . $myTrackPan . '&tilt=' . $myTrackTilt) ){                                                         
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else $this->_redirect('/');
    }
    
    public function centerTrackAction()
    {
    	$get = Zend::registry('fGet');
        $myThreadId = $get->getRaw('threadId');

        if ( $myResponse = @file(C_CONTROL_MOTION . '/' . $myThreadId . '/track/set?x=0&y=0') ){                                                         
            $this->_redirect('/camera?threadId=' . $myThreadId);
        } else $this->_redirect('/');
    }    
    
    public function noRouteAction()
    {
        $this->_redirect('/');
    }
}
?>
